package com.lynx.core.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.text.MessageFormat;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.lynx.core.ApiModel;
import com.lynx.core.ApiResult;
import com.lynx.core.ApiUser;
import com.lynx.core.View;
import com.lynx.core.XmlPushMap;
import com.lynx.core.type.PathType;
import com.lynx.core.type.PatternType;
import com.lynx.core.util.ExtractParamUtil;
import com.lynx.core.util.PushBeanUtils;
/**
 * 
 * @author lxp
 *
 */
public class ViewRouter {
	public static void processor(FilterChain chain,HttpServletRequest request,
			HttpServletResponse response, String keyUrl,String postfix) throws IOException, ServletException {
		ApiModel apiClsInfo = XmlPushMap.APIMAPS.get(keyUrl);
		if(!StringUtils.equals(apiClsInfo.getPostfix(),postfix)){
			chain.doFilter(request, response);
			System.out.println(MessageFormat.format("此方法只能以{0}后缀访问，当前后缀:{1}",apiClsInfo.getPostfix(),postfix));
			return;
		}
		String type = request.getMethod().toLowerCase();
		if(!StringUtils.equals(apiClsInfo.getType(),PathType.all.name())&&
				!StringUtils.equals(apiClsInfo.getType(),type)){
			System.out.println(MessageFormat.format("此方法只能以{0}提交方式访问，当前提交方式:{1}",apiClsInfo.getType(),type));
			chain.doFilter(request, response);
			return;
		}
		try {
			Class<?> cls = Class.forName(apiClsInfo.getClassName());
			ApiResult result = new ApiResult();
			result.setRequest(request);
			result.setResponse(response);
			Method m = cls.getMethod(apiClsInfo.getMethodName(), new Class[] {ApiResult.class, ApiUser.class });
			Class<?> modelClass = apiClsInfo.getModel();
			if (modelClass != null) {
				Object modelobj = modelClass.newInstance();
				PushBeanUtils.push(ExtractParamUtil.getParams(request), modelobj);
				PushBeanUtils.push(ExtractParamUtil.getParamStrs(request), modelobj);
				result.setModel(modelobj);
			}
			View view = null;
			if(StringUtils.equals(apiClsInfo.getPattern(), PatternType.Singleton.name())){
				view = (View) m.invoke(cls, (Object) result, new ApiUser());
			}else{
				view = (View) m.invoke(cls.newInstance(), (Object) result, new ApiUser());
			}
			if (view != null) {
				view.render(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void processor(HttpServletRequest request,
			HttpServletResponse response, String keyUrl,String postfix) throws Exception {
		ApiModel apiClsInfo = XmlPushMap.APIMAPS.get(keyUrl);
		if(!StringUtils.equals(apiClsInfo.getPostfix(),postfix)){
			throw new Exception(MessageFormat.format("此方法只能以{0}后缀访问，当前后缀:{1}",apiClsInfo.getPostfix(),postfix));
		}
		String type = request.getMethod().toLowerCase();
		if(!StringUtils.equals(apiClsInfo.getType(),PathType.all.name())&&
				!StringUtils.equals(apiClsInfo.getType(),type)){
			throw new Exception(MessageFormat.format("此方法只能以{0}提交方式访问，当前提交方式:{1}",apiClsInfo.getType(),type));
		}
		Class<?> cls = Class.forName(apiClsInfo.getClassName());
		ApiResult result = new ApiResult();
		result.setRequest(request);
		result.setResponse(response);
		Method m = cls.getMethod(apiClsInfo.getMethodName(), new Class[] {ApiResult.class, ApiUser.class });
		Class<?> modelClass = apiClsInfo.getModel();
		if (modelClass != null) {
			Object modelobj = modelClass.newInstance();
			PushBeanUtils.push(ExtractParamUtil.getParams(request), modelobj);
			PushBeanUtils.push(ExtractParamUtil.getParamStrs(request), modelobj);
			result.setModel(modelobj);
		}
		View view = null;
		if(StringUtils.equals(apiClsInfo.getPattern(), PatternType.Singleton.name())){
			view = (View) m.invoke(cls, (Object) result, new ApiUser());
		}else{
			view = (View) m.invoke(cls.newInstance(), (Object) result, new ApiUser());
		}
		if (view != null) {
			view.render(request, response);
		}
	}
	
}
